/*
 * Copyright 2008 Nils Adermann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef COUCHDBPP_SESSION_H
#define COUCHDBPP_SESSION_H

#include <string>
#include "curl/curl.h"

#include "couchdb++/JSON/Parser.h"

namespace Couch
{
	class Database;

	class Session
	{
	public:
		Session(const std::string& host, int port = 5984);
		Session(const std::string& host, bool ssl, int port = 5984);
		Session(const std::string& host, const std::string& username, const std::string& password, int port = 5984);
		Session(const std::string& host, const std::string& username, const std::string& password, bool ssl, int port = 5984);
		~Session();

		void setVerbose(bool verbose);

		Database* getDatabase(const std::string& name);

		std::string buildUrl(const std::string& relativeUrl);
		JSON::Value* get(const std::string& relativeUrl);
		
	private:
		void init();
		void initEasyhandle();

		static size_t staticWriteDataCallback(void *ptr, size_t size, size_t nmemb, void* session);
		size_t writeDataCallback(void* ptr, size_t, size_t nmemb);

		CURL* easyhandle;
		char* curlMessage;

		JSON::Parser jsonParser;

		std::string baseUrl;
		std::string host;
		std::string username;
		std::string password;
		int port;
		bool ssl;
		bool useAuth;
		bool verbose;
	};
}

#endif
